package com.eiei.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间序列工具，用来生成对应用户的主键
 * @author eiei
 *
 */
public class TimeSequenceUtils {
	
	private static Integer randInt = 0;
	
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	
	/**
	 * 循环获取时间序列随机数
	 * @return
	 */
	private static String getRandIntString(){
		synchronized (randInt) {
			if(randInt==9999){
				randInt = 0;
			}else{
				randInt++;
			}
			if(randInt<10){
				return "000"+randInt;
			}else if(randInt<100){
				return "00"+randInt;
			}else if(randInt < 1000){
				return "0"+randInt;
			}else{
				return ""+randInt;
			}
		}
	}
	
	/**
	 * 获取主键的字符串
	 * @return
	 */
	public static String getIdString(){
		StringBuilder sb = new StringBuilder();
		sb.append(sdf.format(new Date())).append(getRandIntString());
		return sb.toString();
	}
	
}
